/**
 * ConcretePeerSamplingService.java
 * ************************** 
 * @date Mar 18, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.pss;

import networkRmi.NetworkManager;
import gossipServices.basic.view.PartialView;
import gossipServices.basic.view.PartialView.PEER_SELECTION_ENUM;
import gossipServices.gossipThread.ActiveGossipThread;
import gossipServices.gossipThread.GossipBasedProtocol;
import gossipServices.gossipThread.PassiveGossipThread;
import gossipServices.network.NetworkServicesAPI;

/**
 * 
 * Description: 
 * This class is the starting point of the Peer Sampling Service. 
 * 
 * Needs some parameters in input to the constructor:
 *  - h = the "Healer" number of peer in the PartialView,
 *  - s = the "Swapper" number of peer in the PartialView,
 *  - push = specifying the view propagation strategy,
 *  - pull = specifying the view propagation strategy,
 *  - peerSelection = specifies the peer selection strategy (rand or tail),
 *  - PartialView = a pointer to the local PartialView,
 *  - netMan = a pointer to the local NetworkManager. 
 * 
 * After a successful creation it's possible to launch the 
 * Peer Sampling Service through the method: init();
 * 
 */
public class ConcretePeerSamplingService extends PeerSamplingService {
	
	private ActiveGossipThread activePSST;
	private PassiveGossipThread passivePSST;
	private NetworkServicesAPI nia;	
	
	private boolean showView = false;
	
	private final int CYCLE_LENTHG = 1000;//ms
	
	public ConcretePeerSamplingService(int h, int s, boolean push, boolean pull,
			PEER_SELECTION_ENUM peerSelection, PartialView view, 
			NetworkManager netMan) {
		super();
		this.view = view;
		c = view.size();
		H = h;
		S = s;
		this.peerSelection = peerSelection;
		this.pull = pull;
		this.push = push;
		nia = new PssServicesNetManager(netMan);
	}
	
	@Override
	public boolean init() {
		activePSST = new ActivePSSThread("pss1-Active", CYCLE_LENTHG, view, nia, H, S, push, pull);
		passivePSST = new PassivePSSThread("pss1-Passive", CYCLE_LENTHG, view, nia, H, S, push, pull);
		if(showView)
			showView = ((PassivePSSThread)passivePSST).enableShowView();
		System.out.println("PSS Show View: "+showView);
		setGossipProtocol(new GossipBasedProtocol(activePSST, passivePSST));		
		gossipProtocol.start();
		return false;
	}

	/**
	 * @param b
	 */
	public boolean showView(boolean b) {
		this.showView = b;
		return showView;
	}

	@Override
	public Class<?> getServiceClass() {
		return PeerSamplingServiceAPI.class;
	}	

}
